<template>
    <div class="dv-charts-container" :ref="ref">
        <div class="charts-canvas-container" :ref="chartRef" />
    </div>
</template>

<script>
    import Charts from '@jiaminghi/charts'
    export default {
        name: 'DvCharts',
        props: {
            option: {
                type: Object,
                default: () => ({})
            }
        },
        data () {
            const id ='111'
            return {
                ref: `charts-container-${id}`,
                chartRef: `chart-${id}`,
                chart: null
            }
        },
        watch: {
            option () {
                let { chart, option } = this
                if (!chart) return
                if (!option) option = {}
                chart.setOption(option, true)
            }
        },
        created() {
            this.initChart ()
        },
        methods: {

            initChart () {
                console.log(11111)
                const { $refs, chartRef, option } = this
                const chart = this.chart = new Charts($refs[chartRef])
                if (!option) return
                chart.setOption(option)
            },
            onResize () {
                const { chart } = this
                if (!chart) return
                chart.resize()
            }
        }
    }
</script>

<style lang="scss">
    .dv-charts-container {
        position: relative;
        width: 100%;
        height: 100%;
        .charts-canvas-container {
            width: 100%;
            height: 100%;
        }
    }
</style>
